package com.yongcheon.sms_manager.data.sms_spam;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.provider.Telephony.Sms;
import android.provider.Telephony.Sms.Inbox;
import android.telephony.SmsMessage;

import com.yongcheon.sms_manager.database.SpamDBManager;
import com.yongcheon.sms_manager.utils.StringUtil;
import com.yongcheon.sms_manager.vo.SMSVO;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Created by yongcheon on 2014-12-17.
 */
public class SMS_Spam_Data {

    public static void deleteSMS_insertSpam(ArrayList<SMSVO> smsList, Context context, SMSData smsData, SpamData spamData){
        SQLiteDatabase database = SpamDBManager.getDatabase(context);
        try {
            database.beginTransaction();
            for (int i = 0; i < smsList.size(); i++) {
                spamData.insert(smsList.get(i), context, database);
            }
            for (int i = 0; i < smsList.size(); i++) {
                smsData.delete(smsList.get(i).get_Id(), context,  null);
            }
            database.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            database.endTransaction();
        }
    }

    public static void deleteSMS_insertSpamById(SMSVO smsvo, Context context, SMSData smsData, SpamData spamData){
        ArrayList<SMSVO> smsList = smsData.selectById(String.valueOf(smsvo.get_Id()), context,  null);
        deleteSMS_insertSpam(smsList, context, smsData, spamData);
    }

    public static void deleteSMS_insertSpamAll(SMSVO smsvo, Context context, SMSData smsData, SpamData spamData){
        ArrayList<SMSVO> smsList = smsData.select(smsvo.getAddress(), context,  null);
        for(int i=0; i<smsList.size(); i++){
            smsList.get(i).setSpam_category(smsvo.getSpam_category());
            smsList.get(i).setSpam_desc(smsvo.getSpam_desc());
        }
        deleteSMS_insertSpam(smsList, context, smsData, spamData);
    }

    public static void storeMsg(Context context, Object[] pdusObj, int error){
        String ACTION_NOTIFY_DATASET_CHANGED = "com.android.mms.intent.action.ACTION_NOTIFY_DATASET_CHANGED";

        SmsMessage sms = SmsMessage.createFromPdu((byte[]) pdusObj[0]);
        ContentValues values = extractContentValues(sms);
        values.put(Sms.ERROR_CODE, error);

        String messageBody = "";
        for (int i = 0; i < pdusObj.length; i++) {
            messageBody += SmsMessage.createFromPdu((byte[]) pdusObj[i]).getMessageBody();
        }
        values.put(Inbox.BODY, StringUtil.replaceFormFeeds(messageBody));

        ContentResolver resolver = context.getContentResolver();
        resolver.insert(SMSData.CONTENT_URI_INBOX, values);

        Intent intent = new Intent(ACTION_NOTIFY_DATASET_CHANGED);
        context.sendBroadcast(intent);

    }

    public static ContentValues extractContentValues(SmsMessage sms) {
        ContentValues values = new ContentValues();
        values.put(Inbox.ADDRESS, sms.getDisplayOriginatingAddress());

        Calendar buildDate = new GregorianCalendar(2011, 8, 18);    // 18 Sep 2011
        Calendar nowDate = new GregorianCalendar();
        long now = System.currentTimeMillis();
        nowDate.setTimeInMillis(now);

        if (nowDate.before(buildDate)) {
            now = sms.getTimestampMillis();
        }

        values.put(Inbox.DATE, new Long(now));
        values.put(Inbox.DATE_SENT, Long.valueOf(sms.getTimestampMillis()));
        values.put(Inbox.PROTOCOL, sms.getProtocolIdentifier());
        values.put(Inbox.READ, 0);
        values.put(Inbox.SEEN, 0);
        if (sms.getPseudoSubject().length() > 0) {
            values.put(Inbox.SUBJECT, sms.getPseudoSubject());
        }
        values.put(Inbox.REPLY_PATH_PRESENT, sms.isReplyPathPresent() ? 1 : 0);
        values.put(Inbox.SERVICE_CENTER, sms.getServiceCenterAddress());
        return values;
    }

}
